<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="登录账号:" prop="loginName" label-width="5rem">
        <el-input
          v-model="queryParams.loginName"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户昵称:" prop="userName" label-width="6rem">
        <el-input
          v-model="queryParams.userName"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="测评量表标题：" prop="scaleTitle" label-width="7rem">
        <el-input
          v-model="queryParams.scaleTitle"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="部门:" prop="deptName" label-width="5rem">
        <el-input
          v-model="queryParams.deptName"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="总分：" prop="countScore" label-width="6rem">
        <el-input
          v-model="queryParams.countScore"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:result_personal:edit']"
        >详情</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:result_personal:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:result_personal:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table class="ellipsis" v-loading="loading" :data="result_personalList" @selection-change="handleSelectionChange" tooltip-effect="dark myTooltips">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="测评结果id" align="center" prop="resultId" />
      <el-table-column label="登录名" align="center" prop="loginName" />
      <el-table-column label="姓名" align="center" prop="userName" />
      <!-- <el-table-column label="测评量表id" align="center" prop="scaleId" /> -->
      <!-- <el-table-column label="用户id" align="center" prop="userId" /> -->
      <!-- <el-table-column label="班级" align="center" prop="classesName" /> -->
      <el-table-column label="部门" align="center" prop="deptName" />
      <el-table-column label="量表名称" align="center" prop="scaleTitleShow" />
      <!-- <el-table-column label="总分" align="center" prop="countScore" />
      <el-table-column label="预警分数" align="center" prop="scale.earlyWarningScore" /> -->
      <el-table-column label="预警级别" align="center">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_dangous_level" :value="scope.row.riskLevel" />
          <!-- {{ getWarningLevel(scope.row.scale.earlyWarningScore, scope.row.countScore) }} -->
        </template>
      </el-table-column>
      <el-table-column label="提交时间" align="center" prop="createTime" />
      <!-- <el-table-column label="因子分数" align="center" prop="factorScore" /> -->
      <!-- <el-table-column label="分数范围和结果提示" align="center" prop="resultContent" :show-overflow-tooltip=true /> -->
      <!-- <el-table-column label="因子的分数范围及结果提示" align="center" prop="factorResult" :show-overflow-tooltip=true /> -->
      <!-- <el-table-column label="用户选择数据" align="center" prop="userAnswer" :show-overflow-tooltip=true /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="gotoReport(scope.row)">查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:result_personal:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- <div>
        注：1.危机预警划分的预警级别主要是指个体现阶段危机程度，一级到五级严重程度逐级递减。
    </div>
    <div>
        2.为了不影响正常作答，危机预警结果将在个体完成作答后的5分钟之后显示，请您耐心等候。
    </div> -->
    
  </div>
</template>

<script>
import { listResult_personal, getResult_personal, delResult_personal, addResult_personal, updateResult_personal } from "@/api/mentalTest/personScore.js";
import { getInfo } from "@/api/login"
export default {
  name: "suicide",
  dicts: ['sys_dangous_level'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 个人测评成绩报告表格数据
      result_personalList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //获取总用户id
      userID:null,
      querydata:null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        scaleId: null,
        scaleTitle: null,
        scaleTitleShow:null,
        userId: null,
        loginName: null,
        userName: null,
        countScore: null,
        factorScore: null,
        resultContent: null,
        factorResult: null,
        userAnswer: null,
        classesName: null,
        deptName: null,
        createTime:null,
        riskLevel:0,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  mounted() {
  },
  methods: {
    // /** 查询个人测评成绩报告列表 */
    // async getList() {
    //   this.loading = true;
    //   await getInfo().then(response1 =>{
    //     this.userID = response1.user.userId;
    //     console.log("用户",response1.user);
    //   });
    //   // const userId = this.$route.params && this.$route.params.userId;
    //   console.log("1111",this.userID);
    //   this.queryParams.userId = this.userID;
    //   console.log("请求参数1",this.queryParams);
    //   listResult_personal(this.queryParams).then(response => {
    //     console.log("请求参数2",this.queryParams);
    //     this.result_personalList = response.rows;
    //     console.log("333",this.result_personalList);
    //     this.total = response.total;
    //     this.loading = false;
    //   });
    // },
    /** 查询所有人测评成绩报告列表 */
    getList() {
      this.loading = true;
      this.queryParams.scaleTitle = "自杀可能量表（SPS）";
      listResult_personal(this.queryParams).then(response => {
        console.log("111",response.rows);
        this.result_personalList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        resultId: null,//测评结果id
        scaleId: null,//测评量表id
        scaleTitle: null,//测评量表名称
        scaleTitleShow:null,
        userId: null,//用户id
        loginName: null,//登录账号
        userName: null,//用户昵称
        countScore: null,//总分
        factorScore: null,//因子的分数
        resultContent: null,//分数范围和结果提示
        factorResult: null,//因子的分数范围及结果提示
        userAnswer: null,//用户选择数据
        createTime: null,//创建时间
        updateTime: null,//更新修改时间
        classesName: null,//班级
        deptName: null,//学院
        riskLevel:null,
      };
      this.resetForm("form");
    },
    getWarningLevel(warningScore, totalScore) {
      const percentage = (totalScore / warningScore) * 100;
      if (percentage >= 80) {
        return '一级';
      } else if (percentage >= 60) {
        return '二级';
      } else if (percentage >= 40) {
        return '三级';
      } else if (percentage >= 20) {
        return '四级';
      } else {
        return '五级';
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.resultId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加个人测评成绩报告";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const resultId = row.resultId || this.ids
      getResult_personal(resultId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改个人测评成绩报告";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.resultId != null) {
            updateResult_personal(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addResult_personal(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const resultIds = row.resultId || this.ids;
      this.$modal.confirm('是否确认删除个人测评成绩报告编号为"' + resultIds + '"的数据项？').then(function() {
        return delResult_personal(resultIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.$modal.confirm('确认导出所有危机预警信息吗？').then(response => {
        this.download('system/result_personal/export', {
          ...this.queryParams
        }, `result_personal_${new Date().getTime()}.xlsx`)
      })
    },
    
    //跳转到报告
    gotoReport(row) {
      // console.log("row.earlyWarningScore",row.scale.earlyWarningScore);
      this.$router.push({
        path: '/crisisIntervention/report',
        query: {
            resultId: row.resultId,
            earlyWarningScore:row.scale.earlyWarningScore,
        }
      })
    },
  }
}
</script>
<style>
.myTooltips{
  background-color: rgb(108, 195, 230)!important;
   color: #fff;
   border: 1px solid #333;
   max-width: 30%;
   text-indent:2em;
}
</style>

